package com.bsh.controller.merchant;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.dto.MerchantListQryDto;
import com.bsh.mybatis.model.Merchant;
import com.bsh.mybatis.model.MerchantImg;
import com.bsh.service.MerchantImageService;
import com.bsh.service.MerchantService;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.bsh.utils.UploadUtils;
import com.mysql.jdbc.StringUtils;

/**
 * 邀请返利
 * 
 * @author gsh
 *
 */
@Controller
public class MerchantImageController {
	private static Logger log = LoggerFactory.getLogger(MerchantImageController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	MerchantImageService merchantImgServ;
	@Autowired
	MerchantService merchantServ;
	
	/**
	 * 商户展示图片列表
	 */
	@RequestMapping(value = "/merchant/showimage/list")
	public ModelAndView showImageList(@ModelAttribute MerchantListQryDto qry) {
		String title = request.getParameter("title") == null ? null : request.getParameter("title").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/merchant/showImageList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = merchantImgServ.getPage(pager, title,qry);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("title", title);
		mv.addObject("type", "index");
		mv.addObject("qry", qry);
		return mv;
	}


	/**
	 * 修改商户展示图片--初使化
	 */
	@RequestMapping(value = "/merchant/showimage/edit/{id}")
	public ModelAndView edit(@PathVariable(value = "id") Integer id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/merchant/showImageEdit");
		MerchantImg banner = merchantImgServ.getMerchantImg(id);
		Merchant merchant=merchantServ.getMerchant(banner.getMerchantId());
		
		mv.addObject("mimg", banner);
		mv.addObject("merchantName", merchant.getMerchantName());
		return mv;
	}

	/**
	 * 修改商户展示图片--保存
	 * 
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@RequestMapping(value = "/merchant/showimage/edit/save")
	public ModelAndView editSave(@RequestParam(value = "upimg") MultipartFile file,
			@ModelAttribute MerchantImg banner) throws IllegalStateException, IOException {		
		String fileUrl= "";
		if (!file.isEmpty()) {
			fileUrl = UploadUtils.save(file, "/upload/public/merchant/show");
			banner.setUrl(fileUrl);
		}		
		Integer res = merchantImgServ.editMerchantImg(banner,!file.isEmpty());
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/merchant/showImageEdit");
		mv.addObject("mimg", banner);

		String msg = PageTools.alertCloseFraRefresh("修改商户展示图片", "保存成功！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}

	/**
	 * 添加商户展示图片--初使化
	 */
	@RequestMapping(value = "/merchant/showimage/add/{type}")
	public ModelAndView add(@PathVariable(value="type")String type) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/merchant/showImageAdd");
		mv.addObject("type", type);
		return mv;
	}

	/**
	 * 添加商户展示图片--保存
	 */
	@RequestMapping(value = "/merchant/showimage/add/save")
	public ModelAndView addSave(@RequestParam(value = "upimg") MultipartFile file,
			@ModelAttribute MerchantImg banner) throws IllegalStateException, IOException {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/merchant/showImageAdd");
		mv.addObject("mimg", banner);
		
		String fileUrl= "";
		if (!file.isEmpty()) {
			fileUrl = UploadUtils.save(file, "/upload/public/merchant/show");
			banner.setUrl(fileUrl);
		}
		if(banner.getMerchantId()==null){
			String msg = PageTools.alert("添加商户展示图片", "请选择商户！", 1000);
			mv.addObject("actionMsg", msg);
			return mv;
		}
		Integer res = merchantImgServ.addMerchantImg(banner);

		String msg = PageTools.alertCloseFraRefresh("添加商户展示图片", "保存成功！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}

	/**
	 * 查看展示图片
	 */
	@ResponseBody
	@RequestMapping(value = "/merchant/showimage/view/{id}")
	public PageJosnMessage view(@PathVariable(value = "id") Integer id) {
		MerchantImg banner = merchantImgServ.getMerchantImg(id);
		PageJosnMessage msg = new PageJosnMessage();
		msg.putData("mimg", banner);
		return msg;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping(value = "/merchant/showimage/delete/{id}")
	public PageJosnMessage delete(@PathVariable(value = "id") Integer id) {
		Integer res = merchantImgServ.deleteMerchantImg(id);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}
	
	
	/**
	 * 查看商家的所有展示图片
	 */
	@ResponseBody
	@RequestMapping(value = "/merchant/images/{id}")
	public ModelAndView viewMerchantImages(@PathVariable(value = "id") Integer mid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/merchant/showImageOfMerchant");
		List<MerchantImg> images = merchantImgServ.getMerchantImgList(mid);
		
		mv.addObject("images", images);
		return mv;
	}
}
